package com.dk.controller;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.MessageQueueSelector;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import com.dk.mq.RocketMQProducer;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabUser;
import com.dk.service.CollectService;
import com.dk.service.RouteService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("collectController")
public class CollectController {
    @Autowired
    CollectService collectService;
    @Autowired
    RouteService routeService;
    @Autowired
    RocketMQProducer rocketMQProducer;



    @RequestMapping("collect")
    @ResponseBody
    public TabRoute collect(@RequestParam("rid")Integer rid, HttpServletRequest request){
        HttpSession session = request.getSession();
        TabUser tabUser = (TabUser) session.getAttribute("userLogin");
        TabRoute tabRoute = routeService.getInfoByRid(rid);
        tabRoute.setCount(tabRoute.getCount()+1);
        //数据库实时更新收藏数据
        routeService.updateRoute(tabRoute);
        //通过mq进行收藏表更新
        List<Integer> list = new ArrayList<>();
        list.add(rid);
        list.add(tabUser.getUid());
        Gson gson = new Gson();
        String s = gson.toJson(list);
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        Message msg = new Message("dkjy",bytes);
        SendResult sendResult = null;
        try {
            sendResult = rocketMQProducer.getDefaultMQProducer().send(msg, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    return list.get((Integer) o);
                }
            },0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(sendResult);
        return tabRoute;
    }

    @RequestMapping("cancelCollect")
    @ResponseBody
    public boolean cancelCollect(Integer rid,Integer uid){
        int i = collectService.deleteCancelCollect(rid,uid);
        return i>0;
    }

    /*@RequestMapping("sendMsg")
    @ResponseBody
    public String sendMsg() throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
        Integer rid = 1;
        Integer sid = 2;
        Map<String,Object> map = new HashMap<>();
        map.put("rid",rid);
        map.put("sid",sid);
        Gson gson = new Gson();
        String s = gson.toJson(map);
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        Message msg = new Message("dkjy",bytes);
        SendResult sendResult = rocketMQProducer.getDefaultMQProducer().send(msg, new MessageQueueSelector() {
            @Override
            public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                return list.get((Integer) o);
            }
        },0);
        System.out.println(sendResult);
        return "success";
    }*/
}
